package com.ruoyi.scale.document.refs;

import lombok.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ScaleDomainResult {
    /**
     * 关联领域数据
     */
    private ScaleDomainData scaleDomainData;
    /**
     * 领域名
     */
    private String domainName;
    /**
     * 题目数量
     */
    private Integer questionNum;
    /**
     * 分数
     */
    private BigDecimal score;
    /**
     * 项目平均分数
     */
    private BigDecimal avgScore;

    /**
     * 结论
     */
    private String conclusion;
    /**
     * 简单建议
     */
    private String advice;
    /**
     * 详细建议
     */
    private String detailAdvice;
    /**
     * 参考
     */
    private String reference;

    public boolean matchAnswer(Integer no) {
        return scaleDomainData.getQuestionNos().contains(no);
    }

    /**
     * 累加分数
     *
     * @param score
     */
    public void accrualScore(BigDecimal score) {
        this.score = this.score == null ? BigDecimal.ZERO : this.score;
        this.score = this.score.add(score);
    }

    /**
     * 匹配计分阶段(多个)
     *
     * @return
     */
    public List<ScaleDomainRule> matchDomain() {
        return scaleDomainData.getScaleDomainRules().stream().filter(t -> t.match(this.score)).collect(Collectors.toList());
    }

    /**
     * 匹配计分阶段(单个)
     *
     * @return
     */
    public ScaleDomainRule matchFirstDomainRule() {
        for (ScaleDomainRule scaleDomainRule : scaleDomainData.getScaleDomainRules()) {
            if (scaleDomainRule.match(this.score)) {
                return scaleDomainRule;
            }
        }
        return null;
    }

    public void avg() {
        this.avgScore = this.score.divide(new BigDecimal(this.questionNum), 2,RoundingMode.HALF_UP);
    }
}
